<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Mmod_upload extends MYAD_Model{
    function __construct(){
        parent::__construct();
        session_start();
    }
/*
 * @author :        HungLV
 * @created:        
 * @Description:    Upload file csv
 * @Input :         Null
 * @Output :        
 */     
    function uploadCSV(){
        $temp = array();
        $this->form_validation->set_error_delimiters('', '');
        $this->form_validation->set_rules('txtfile',$this->lang->line('upload_fileupload'),'required');
        if($this->input->post('txtfile')){
           $temp['txtfile'] = $this->input->post('txtfile');
        }
        else{
            $temp['txtfile'] = set_value('txtfile');
        }
        if($this->form_validation->run() == TRUE){ 
            // extension csv 
            $csv_mimetypes = array('csv','txt');
            // check extentsion csv
            if (in_array(end(explode('.', $_FILES['fileupload']['name'])), $csv_mimetypes)) {
                 // check open temp file
                if (($handle = fopen($_FILES['fileupload']['tmp_name'], "r")) !== FALSE) {                    
                    // check content format file upload csv
                    if($this->checkEmptyFile($handle)){
                        //open temp file
                        $handle1 = fopen($_FILES['fileupload']['tmp_name'], "r");
                        if($this->checkFormatCsv($handle1)){
                            // delete session 
                            unset($_SESSION['arrCard']); 
                            // open read temp file upload
                            $handle2 = fopen($_FILES['fileupload']['tmp_name'], "r");
                            $a = 0;
                            $arr = array();
                            // read file and save array
                            while (!feof($handle2)){
                                ++$a;
                                $arr[$a] = fgetcsv($handle2);
                            }
                            fclose($handle2);
                            // save data to session
                            $_SESSION['arrCard'] = array_filter($arr);
                            $temp['flag'] = TRUE;
                            $temp['message'] = $this->lang->line('upload_uploadsuccess');
                        }
                        else{
                            $temp['flag'] = FALSE;
                            $temp['message'] = $this->lang->line('upload_csvError');                            
                        }
                    }
                    else{
                        $temp['flag'] = FALSE;
                        $temp['message'] = $this->lang->line('upload_fileEmpty');                            
                    }                    
                }
                else{
                    $temp['flag'] = FALSE;
                    $temp['message'] = $this->lang->line('upload_noReadFile');                            
                }                 
            }
            else{
                $temp['flag'] = FALSE;
                $temp['message'] = $this->lang->line('upload_extensionError');                            
            }            
        }
        return $temp;
    }
    /**
     * Function check format file csv
     * @param handle $handle
     * @return true and false
     */
  function checkFormatCsv($handle){
           while (!feof($handle)) {
               $line = fgets($handle);
               if(strlen(trim($line)) > 0){
                    if(substr_count($line, ",")!=5)
                    {
                        return false;
                    }
               }
           }
           fclose($handle);
           return true;        
    }
/*
 * @author :        HungLV
 * @created:        
 * @Description:    Get info for view
 * @Input :         Null
 * @Output :        return array for view
 */   
    function  getInfoUpload(){
        $temp = array(
            'show' => 'index',
            'js' => array('jquery-ui.js'),
            'css' => array('jquery-ui.css'),
            'title'=> $this->lang->line('upload_title'),
        );
        return $temp;
    } 
/*
 * @author :        HungLV
 * @created:        
 * @Description:    Get info for view
 * @Input :         Null
 * @Output :        return array for view
 */   
    function  checkEmptyFile($handle){
        while (!feof($handle)) {
            $line = fgets($handle);
            if(strlen(trim($line)) > 0){
                return TRUE;
            }
        }
        return FALSE;
    }    
}
?>
